// 封装cookie对象
// 使用：
// cookieUtil.set('user','lala',setCookieDate(7));
// cookieUtil.set('user')

const cookieUtil = {
	set:(name,value,expires,path,domain,secure) => {
		let cookieText=encodeURIComponent(name)+'='+ encodeURIComponent(value)

		if(expires instanceof Date){
			cookieText+=';expires='+expires.toGMTString()
		}
		if(path){
			cookieText += ';path='+path
		}
		if(domain){
			cookieText += ';domain='+domain
		}
		if(secure){
			cookieText += ';secure'
		}
		document.cookie=cookieText
	},
		get:(name) => {
		let cookieName=encodeURIComponent(name)+'=',
			cookieStart=document.cookie.indexOf(cookieName),
			cookieValue=null

		if(cookieStart > -1){
			let cookieEnd = document.cookie.indexOf(';',cookieStart);
			if(cookieEnd == -1){
				cookieEnd = document.cookie.length
			}
			cookieValue = decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd))
		}
		return cookieValue
	},
		unset:(name,path,domain,secure)=> {
		this.set(name,'',new Date(0),domain,path)
	}
}

export default cookieUtil